package com.yf.serviceImpl;

import com.yf.bean.AnswerBean;
import com.yf.bean.ExamBean;
import com.yf.bean.SolveLogBean;
import com.yf.dao.AnswerDao;
import com.yf.dao.ExamDao;
import com.yf.dao.SolveLogDao;
import com.yf.entity.SolveLogEntity;
import com.yf.service.RecordLogService;
import com.yf.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service("RecordLogServiceImpl")
public class RecordLogServiceImpl implements RecordLogService {
    @Autowired
    SolveLogDao logDao;

    @Autowired
    ExamDao examDao;

    @Autowired
    AnswerDao answerDao;

    @Override
    public int addRecord(List<SolveLogBean> records) {
        return logDao.addLog(records);
    }

    @Override
    public List<SolveLogEntity> getSolveLog(Long uid) {
        final List<SolveLogBean> recentLog = logDao.getRecentLog(uid);
        List<SolveLogEntity> ans = null;
        List<ExamBean> examBeans = null;
        if (recentLog != null && recentLog.size() > 0) {
            List<Long> ids = new ArrayList<>(recentLog.size());
            for (SolveLogBean bean: recentLog) {
                ids.add(bean.getPid());
            }

            examBeans = examDao.getQusById(ids);
            if (examBeans != null) {

                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                ans = new ArrayList<>(recentLog.size());
                for (int i = 0; i < examBeans.size(); i++) {
                    SolveLogBean log = recentLog.get(i);
                    ExamBean exam = examBeans.get(i);

                    SolveLogEntity e = new SolveLogEntity();
                    e.setPid(exam.getId());
                    e.setYear(exam.getYear());
                    e.setNumber(exam.getNumber().toString());
                    e.setTag(exam.getTag());
                    e.setType(exam.getType());
                    e.setSubject(exam.getSubject());
                    e.setCorrect(log.getStatus() == 1);
                    e.setCreated(simpleDateFormat.format(log.getCreated()));
                    ans.add(e);
                }

//                if (ans.size() != recentLog.size()) {
//                    int i = examBeans.size();
//                    while (i < recentLog.size()) {
//                        for (int j=examBeans.size()-1; j>=0; j--) {
//                            if (recentLog.get(i).getPid().equals(ans.get(j).getPid())) {
//                                SolveLogEntity e = new SolveLogEntity(ans.get(j));
//                                ans.add(e);
//                            }
//                        }
//                        i++;
//                    }
//                }
            }
        }

        return ans;
    }

    public ExamBean getQusForLog(Long pid) {
        ExamBean examBean = examDao.getQusForLog(pid);
        List<AnswerBean> onlyAnswerByPid = answerDao.getOnlyAnswerByPid(pid.intValue());
        List<String> ansList = new ArrayList<>(onlyAnswerByPid.size());
        for (AnswerBean answerBean: onlyAnswerByPid) {
            ansList.add(answerBean.getAnswer());
        }
        examBean.setAnswer(String.join(Constant.ansSeq, ansList));
        return examBean;
    }
}
